#include "stdio.h"
#include "SDL/SDL.h"
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#include <SDL/SDL.h>
bool collision(SDL_Rect* rect1,SDL_Rect* rect2)
{
        if(rect1->y >= rect2->y + rect2->h)
                return 0;
        if(rect1->x >= rect2->x + rect2->w)
                return 0;
        if(rect1->y + rect1->h <= rect2->y)
                return 0;
        if(rect1->x + rect1->w <= rect2->x)
                return 0;
        return 1;
}
int width = 640;
int height = 480;
int main(int argc, char** argv)
{int x,y;
    SDL_Surface *menuprincipal;
    SDL_Surface *menuprincipal2;
    SDL_Surface *photoregles;
    SDL_Surface *jeu;
    SDL_Surface *jerry;
    //SDL_Surface *gameover;
    SDL_Surface *screen;
    SDL_Event event;
    int numfenetre ;
    int done=1;
    SDL_Rect src,dest,dest1;
    freopen("CON","w",stdout);
    freopen("CON","r",stdin);
    // initialize SDL video
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        return (-1);
    }
    // create a new window
    screen = SDL_SetVideoMode(646, 469, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (!screen)
    {
        printf("Unable to set 646x469 video: %s\n", SDL_GetError());
        return (-1);
    }
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    menuprincipal = SDL_LoadBMP("menuprincipal.bmp");
    menuprincipal2= SDL_LoadBMP("menuprincipal2.bmp");
    photoregles= SDL_LoadBMP("photoregles.bmp");
    jerry= SDL_LoadBMP("jerry.bmp");
//    gameover= SDL_LoadBMP("gameover.bmp");
    jeu= SDL_LoadBMP ("jeu.bmp");
    src.x = 0;
    src.y = 0;
    src.w = menuprincipal->w; //Use image->w to display the entire width of the image
    src.h = menuprincipal->h; //Use image->h to display the entire height of the image
    dest.x = 0;//
    dest.y = 0;
    dest1.x = 440;//
    dest1.y = 405;
    dest.w = menuprincipal->w;
    dest.h = menuprincipal->h;
    SDL_BlitSurface(menuprincipal, &src, screen, &dest);
    numfenetre=1;
    SDL_Flip(screen);
    while(done)
    {
        if (numfenetre==1)
        {
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    done = 0;
                    break;
// check for keypresses
                case SDL_KEYDOWN:
                {
// exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = 0;
                    break;
                    case SDL_MOUSEBUTTONDOWN:
                         x=event.motion.x;
                        printf("%d",x);
                         y=event.motion.y;
                        if(x<612 && x>32 && y>390 && y<470)
                      {
                            SDL_BlitSurface(menuprincipal2, &src, screen, &dest);
                            SDL_Flip(screen);
                            numfenetre=2;
                            break;
                        }
                    }
                }
            }
        }
        if(numfenetre == 2)
        {
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    done = 0;
                    break;
// check for keypresses
                case SDL_KEYDOWN:
                {
// exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = 0;
                    break;
                    case SDL_MOUSEBUTTONDOWN:
                         x=event.motion.x;
                        printf("%d",x);
                         y=event.motion.y;
            if(x>20 && x<249&& y>377 && y<463)
                        {
                            SDL_BlitSurface(jeu, &src, screen, &dest);
                            SDL_Flip(screen);
                            numfenetre=3;
                        }
                         if(x>257&& x<497&& y>380 && y<463)
                        {
                            SDL_BlitSurface(photoregles, &src, screen, &dest);
                            SDL_Flip(screen);
                            numfenetre=4;
                        }
                         if(x>506 && x<705&& y>381 && y<460)
                        {
                            done=0;
                        }
            }
        }        }
        }
if (numfenetre==3)
        { SDL_Init(SDL_INIT_EVERYTHING);
        SDL_Surface *screen;
             SDL_Surface *victoire;
        screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
        bool running = true;
        const int FPS = 30;
        Uint32 start;
        bool b[4] = {0,0,0,0};
        SDL_Rect rect;
        rect.x = 10;
        rect.y = 60;
        rect.w = 20;
        rect.h = 20;
        SDL_Rect recarr[31];
        recarr[0].x=0;
        recarr[0].y=0;
        recarr[0].h=10;
        recarr[0].w=260;
        recarr[1].x=370;
        recarr[1].y=470;
        recarr[1].h=10;
        recarr[1].w=60;
        recarr[2].x=50;
        recarr[2].y=0;
        recarr[2].h=140;
        recarr[2].w=10;
        recarr[3].x=50;
        recarr[3].y=100;
        recarr[3].h=10;
        recarr[3].w=100;
        recarr[4].x=0;
        recarr[4].y=470;
        recarr[4].h=10;
        recarr[4].w=480;
        recarr[5].x=630;
        recarr[5].y=10;
        recarr[5].h=470;
        recarr[5].w=10;
        recarr[6].x=570;
        recarr[6].y=230;
        recarr[6].h=70;
        recarr[6].w=10;
        recarr[7].x=0;
        recarr[7].y=470;
        recarr[7].h=10;
        recarr[7].w=480;
        recarr[8].x=630;
        recarr[8].y=10;
        recarr[8].h=470;
        recarr[8].w=10;
        recarr[9].x=360;
        recarr[9].y=0;
        recarr[9].h=10;
        recarr[9].w=280;
        recarr[10].x=570;
        recarr[10].y=10;
        recarr[10].h=70;
        recarr[10].w=10;
        recarr[11].x=500;
        recarr[11].y=70;
        recarr[11].h=10;
        recarr[11].w=70;
        recarr[12].x=250;
        recarr[12].y=400;
        recarr[12].h=190;
        recarr[12].w=10;
        recarr[13].x=130;
        recarr[13].y=370;
        recarr[13].h=100;
        recarr[13].w=10;
        recarr[14].x=470;
        recarr[14].y=360;
        recarr[14].h=190;
        recarr[14].w=10;
        recarr[15].x=260;
        recarr[15].y=400;
        recarr[15].h=10;
        recarr[15].w=70;
        recarr[16].x=250;
        recarr[16].y=400;
        recarr[16].h=10;
        recarr[16].w=90;
        recarr[17].x=200;
        recarr[17].y=150;
        recarr[17].h=150;
        recarr[17].w=10;
        recarr[18].x=200;
        recarr[18].y=200;
        recarr[18].h=10;
        recarr[18].w=150;
        recarr[19].x=340;
        recarr[19].y=200;
        recarr[19].h=140;
        recarr[19].w=10;
        recarr[20].x=130;
        recarr[20].y=260;
        recarr[20].h=10;
        recarr[20].w=70;
        recarr[21].x=500;
        recarr[21].y=220;
        recarr[21].h=10;
        recarr[21].w=170;
        recarr[22].x=500;
        recarr[22].y=140;
        recarr[22].h=80;
        recarr[22].w=10;
        recarr[23].x=270;
        recarr[23].y=120;
        recarr[23].h=10;
        recarr[23].w=10;
          recarr[24].x=370;
        recarr[24].y=120;
        recarr[24].h=50;
        recarr[24].w=10;
          recarr[25].x=0;
        recarr[25].y=260;
        recarr[25].h=10;
        recarr[25].w=60;
          recarr[26].x=370;
        recarr[26].y=150;
        recarr[26].h=10;
        recarr[26].w=150;
            recarr[27].x=50;
        recarr[27].y=260;
        recarr[27].h=50;
        recarr[27].w=10;
          recarr[28].x=530;
        recarr[28].y=230;
        recarr[28].h=70;
        recarr[28].w=10;
          recarr[29].x=570;
        recarr[29].y=230;
        recarr[29].h=70;
        recarr[29].w=10;
        recarr[30].x=0;
        recarr[30].y=0;
        recarr[30].h=10;
        recarr[30].w=280;
 SDL_Surface *image1 ;
SDL_Rect position1;
    position1.x = 270;
    position1.y = 0;
    position1.w = 5;
    position1.h = 5;
 SDL_Surface *image2 ;
SDL_Rect position2;
    position2.x = 0;
    position2.y = 0;
    position2.w = 5;
    position2.h = 5;
    SDL_Surface *image3 ;
SDL_Rect position3;
    position3.x = 40;
    position3.y = 340;
    position3.w = 5;
    position3.h = 5;
    SDL_Surface *image4 ;
SDL_Rect position4;
    position4.x = 370;
    position4.y = 270;
    position4.w = 5;
    position4.h = 5;
    SDL_Surface *image5 ;
SDL_Rect position5;
    position5.x = 160;
    position5.y = 320;
    position5.w = 5;
    position5.h = 5;
    SDL_Surface *image6 ;
SDL_Rect position6;
    position6.x = 550;
    position6.y = 130;
    position6.w = 5;
    position6.h = 5;
     SDL_Surface *image7 ;
SDL_Rect position7;
    position7.x = 40;
    position7.y = 140;
    position7.w = 5;
    position7.h = 5;
       SDL_Surface *image8 ;
SDL_Rect position8;
    position8.x = 380;
    position8.y = 60;
    position8.w = 5;
    position8.h = 5;
SDL_Rect positionVictoire;
    positionVictoire.x = 0;
    positionVictoire.y = 0;
 SDL_Surface *gameover ;
SDL_Rect positiongameover;
    positiongameover.x = 0;
    positiongameover.y = 0;
image1 = SDL_LoadBMP("jben.bmp");
image2 = SDL_LoadBMP("fleche.bmp");
image3 = SDL_LoadBMP("jerry1.bmp");
image4 = SDL_LoadBMP("jerry2.bmp");
image5 = SDL_LoadBMP("fantome1.bmp");
image6 = SDL_LoadBMP("fantome2.bmp");
image7 = SDL_LoadBMP("dog1.bmp");
image8 = SDL_LoadBMP("dog2.bmp");
victoire = SDL_LoadBMP("victoire.bmp");
gameover =SDL_LoadBMP("gameover.bmp");
        Uint32 color2 = SDL_MapRGB(screen->format, 3,255,255);
        Uint32 color = SDL_MapRGB(screen->format, 0,0,0);
        for(int i = 0; i < 31; i += 1) {
                        SDL_FillRect(screen,&recarr[i],color2);
        }
        while(running) {
SDL_BlitSurface(image1, NULL, screen, &position1);
SDL_BlitSurface(image2, NULL, screen, &position2);
SDL_BlitSurface(image3, NULL, screen, &position3);
SDL_BlitSurface(image4, NULL, screen, &position4);
SDL_BlitSurface(image5, NULL, screen, &position5);
SDL_BlitSurface(image6, NULL, screen, &position6);
SDL_BlitSurface(image7, NULL, screen, &position7);
SDL_BlitSurface(image8, NULL, screen, &position8);
SDL_Flip(screen);
                start = SDL_GetTicks();
                SDL_Event event;
                while(SDL_PollEvent(&event)) {
                        switch(event.type) {
                                case SDL_QUIT:
                                        running = false;
                                        break;
                                case SDL_KEYDOWN:
                                        switch(event.key.keysym.sym) {
                                                case SDLK_UP:
                                                        b[0] = 1;
                                                        break;
                                                case SDLK_LEFT:
                                                        b[1] = 1;
                                                        break;
                                                case SDLK_DOWN:
                                                        b[2] = 1;
                                                        break;
                                                case SDLK_RIGHT:
                                                        b[3] = 1;
                                                        break;
                                        }
                                        break;
                                case SDL_KEYUP:
                                        switch(event.key.keysym.sym) {
                                                case SDLK_UP:
                                                        b[0] = 0;
                                                        break;
                                                case SDLK_LEFT:
                                                        b[1] = 0;
                                                        break;
                                                case SDLK_DOWN:
                                                        b[2] = 0;
                                                        break;
                                                case SDLK_RIGHT:
                                                        b[3] = 0;
                                                        break;
                                        }
                                        break;
                        }
                }
                SDL_FillRect(screen, &rect, color);
                //logic
                if(b[0]) {
                        rect.y--;
                        for(int i = 0; i < 31; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.y++;
                }
                if(b[1]) {
                        rect.x--;
                        for(int i = 0; i < 31; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.x++;
                }
                if(b[2]) {
                        rect.y++;
                        for(int i = 0; i < 31; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.y--;
                }
                if(b[3]) {
                        rect.x++;
                        for(int i = 0; i < 31; i += 1)
                                if(collision(&rect,&recarr[i]))
                                        rect.x--;
                }
if (rect.x<333&&rect.x>270&&rect.y<54&&rect.y>0) {   SDL_BlitSurface(victoire, NULL, screen, &positionVictoire);   }
if (rect.x<104&&rect.x>40 && rect.y<217&& rect.y>140){ SDL_BlitSurface(gameover, NULL, screen, &positiongameover);   }
                //render
                SDL_FillRect(screen, &rect, color2);
                SDL_Flip(screen);
                if(1000/FPS > SDL_GetTicks()-start) {
                        SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
                }
        }
        SDL_Quit();
        }
        if (numfenetre==4)
        {
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    done = 0;
                    break;
// check for keypresses
                case SDL_KEYDOWN:
                {
// exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = 0;
                    break;
                    case SDL_MOUSEBUTTONDOWN:
                         x=event.motion.x;
                        printf("%d",x);
                         y=event.motion.y;
                        if(x<153 && x>23 && y>39 && y<109)
                      {
                            SDL_BlitSurface(menuprincipal2, &src, screen, &dest);
                            SDL_Flip(screen);
                            numfenetre=2;
                            break;
                        }
                    }
                }
            }
        }
    }
return 0;
        }
